//
//  HBHomeInviteFriendCtrl.m
//  HuoBao
//
//  Created by chaiyanan on 15/12/9.
//  Copyright © 2015年 jinxin. All rights reserved.
//

#import "HBHomeInviteFriendCtrl.h"
#import "HBGetRegInvitingUrlInfo.h"
#import "HBGetRegInvitingUrlParam.h"
#import "HBGetRegInvitingUrlParser.h"
#import "HBGetRegSharingUrlInfo.h"
#import "HBGetRegSharingUrlParam.h"
#import "HBGetRegSharingUrlParser.h"
#import "CustomerShareView.h"
#import "UMSocialQQHandler.h"
#import "UMSocial.h"

@interface HBHomeInviteFriendCtrl ()
<UIWebViewDelegate>

@property (nonatomic, strong) UIWebView *myWebView;
@property (nonatomic, strong) NSString *webUrl;
@property (nonatomic, strong) HBGetRegSharingUrlInfo *shareInfo;



@end

@implementation HBHomeInviteFriendCtrl

#pragma mark - system

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)buildLayout
{
    [super buildLayout];
    self.title = @"邀请好友";
    self.myWebView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64)];
    self.myWebView.delegate = self;
    self.myWebView.backgroundColor = [LCCommon getColor:Main_BackGround_Color];
    self.myWebView.opaque = NO;
    [self.view addSubview:self.myWebView];
    [self requestToGetShareUrl];
    [self requestToGetUrl];
    [self showDoneWithTitle:@"分享"];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:YES];
    
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:YES];
    [self.myWebView stopLoading];
    self.myWebView.delegate = nil;
    [self.myWebView removeFromSuperview];
    self.myWebView = nil;
}

- (void)doDone
{
    [super doDone];
    if (self.shareInfo)
    {
        [[[CustomerShareView sharedCustomerShareView]initWithTitle:@"分享到" cancelButtonTitle:@"取消分享" cancelButtonAction:nil otherButtonAction:^(NSInteger index, CustomerShareView * _Nullable shareView) {
            switch (index) {
                case CustomerShareType_QQ:
                    [self sharetoQQfirend];
                    break;
                case CustomerShareType_QQZone:
                    [self sharetoQQfamily];
                    break;
                case CustomerShareType_Weixin:
                    [self sharetowechat];
                    break;
                case CustomerShareType_QQWeixinTimeLine:
                    [self sharetowechatFamily];
                    break;
                default:
                    break;
            }
            [shareView removeFromSuperview];
        }]showInView];
    }
    else
    {
        DISSMISS_ERR(@"获取分享信息失败");
    }
}

#pragma mark - delegate

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    LLog(@"%@,%@",request.URL.absoluteString,self.webUrl);
    if ([request.URL.absoluteString isEqualToString:self.webUrl]||[request.URL.absoluteString isEqualToString:[NSString stringWithFormat:@"%@/",self.webUrl]])
    {
        [CustomLoadingView showHudToView:self.view];
    }
    return YES;
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [CustomLoadingView hideHudForView:self.view];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    [CustomLoadingView hideHudForView:self.view];
}

#pragma mark - net

- (void)requestToGetUrl
{
    [CustomLoadingView showHudToView:self.view];
    HBGetRegInvitingUrlParam *param = [[HBGetRegInvitingUrlParam alloc]init];
    
    HBGetRegInvitingUrlParser *parser = [[HBGetRegInvitingUrlParser alloc]init];
    [NetCenter getDataFromURL:Method_getRegInvitingUrl params:param parser:parser result:^(ParserResult *myResult) {
        [CustomLoadingView hideHudForView:self.view];
        if (myResult.code==0)
        {
            HBGetRegInvitingUrlInfo *urlInfo = (HBGetRegInvitingUrlInfo *)myResult.result;
            self.webUrl = urlInfo.dataInfo;
            [self.myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.webUrl]]];
        }
        else
        {
            DISSMISS_ERR(myResult.message);
        }
        
    } failure:^(NSError *error) {
        [CustomLoadingView hideHudForView:self.view];
        DISSMISS_ERR([error description]);
    }];
}

- (void)requestToGetShareUrl
{
//    [CustomLoadingView showHudToView:self.view];
    HBGetRegSharingUrlParam *param = [[HBGetRegSharingUrlParam alloc]init];
    
    HBGetRegSharingUrlParser *parser = [[HBGetRegSharingUrlParser alloc]init];
    [NetCenter getDataFromURL:Method_getRegSharingUrl params:param parser:parser result:^(ParserResult *myResult) {
        [CustomLoadingView hideHudForView:self.view];
        if (myResult.code==0)
        {
            HBGetRegSharingUrlInfo *urlInfo = (HBGetRegSharingUrlInfo *)myResult.result;
            self.shareInfo = urlInfo;
            
        }
        else
        {
//            DISSMISS_ERR(myResult.message);
        }
        
    } failure:^(NSError *error) {
//        [CustomLoadingView hideHudForView:self.view];
//        DISSMISS_ERR([error description]);
    }];
}

#pragma mark - share
-(void)sharetowechat{
  
    UIImage *test = [UIImage imageNamed:@"share_image"];
    UMSocialUrlResource *source = nil;
    if (self.shareInfo.regShareImg.length>0)
    {
        source = [[UMSocialUrlResource alloc]initWithSnsResourceType:UMSocialUrlResourceTypeImage url:self.shareInfo.regShareImg];
    }
    [UMSocialData defaultData].extConfig.wechatSessionData.title = self.shareInfo.regShareTitle.length>0?self.shareInfo.regShareTitle:@"金桔金融";
    [UMSocialData defaultData].extConfig.wechatSessionData.url = [NSString stringWithFormat:@"%@?uid=%@",self.shareInfo.regShareUrl,[HBLoginManager sharedHBLoginManager].currentUserInfo.uid];
    
    [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[UMShareToWechatSession] content:self.shareInfo.regShareDesc image:source?nil:test location:nil urlResource:source presentedController:self completion:^(UMSocialResponseEntity *response){
        if (response.responseCode == UMSResponseCodeSuccess) {
            NSLog(@"分享成功！");
        }
    }];
}


-(void)sharetowechatFamily{
 
    UIImage *test = [UIImage imageNamed:@"share_image"];

    UMSocialUrlResource *source = nil;
    if (self.shareInfo.regShareImg.length>0)
    {
        source = [[UMSocialUrlResource alloc]initWithSnsResourceType:UMSocialUrlResourceTypeImage url:self.shareInfo.regShareImg];
    }
    [UMSocialData defaultData].extConfig.wechatTimelineData.title = self.shareInfo.regShareTitle.length>0?self.shareInfo.regShareTitle:@"金桔金融";
    [UMSocialData defaultData].extConfig.wechatTimelineData.url = [NSString stringWithFormat:@"%@?uid=%@",self.shareInfo.regShareUrl,[HBLoginManager sharedHBLoginManager].currentUserInfo.uid];
   
    [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[UMShareToWechatTimeline] content:self.shareInfo.regShareDesc image:source?:test location:nil urlResource:source presentedController:self completion:^(UMSocialResponseEntity *response){
        if (response.responseCode == UMSResponseCodeSuccess) {
            NSLog(@"分享成功！");
        }
    }];
}


-(void)sharetoQQfirend{
    //NSString *token = [LCLoginManager sharedLCLoginManager].currentUserInfo.token;
    UIImage *test = [UIImage imageNamed:@"share_image"];
    //UMSocialUrlResource *source = [[UMSocialUrlResource alloc]initWithSnsResourceType:UMSocialUrlResourceTypeImage url:self.allitem.imgUrl];
    if ([QQApiInterface isQQInstalled]) {
        
        UMSocialUrlResource *source = nil;
        if (self.shareInfo.regShareImg.length>0)
        {
            source = [[UMSocialUrlResource alloc]initWithSnsResourceType:UMSocialUrlResourceTypeImage url:self.shareInfo.regShareImg];
        }
        [UMSocialData defaultData].extConfig.qqData.title = self.shareInfo.regShareTitle.length>0?self.shareInfo.regShareTitle:@"金桔金融";
        [UMSocialData defaultData].extConfig.qqData.url = [NSString stringWithFormat:@"%@?uid=%@",self.shareInfo.regShareUrl,[HBLoginManager sharedHBLoginManager].currentUserInfo.uid];
        
        [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[UMShareToQQ] content:self.shareInfo.regShareDesc image:source?:test location:nil urlResource:source presentedController:self completion:^(UMSocialResponseEntity *response){
            if (response.responseCode == UMSResponseCodeSuccess) {
                NSLog(@"分享成功！");
            }
        }];
    }else{
        UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:@"您还没有安装手机QQ" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alter show];
    }
}


-(void)sharetoQQfamily{

    UIImage *test = [UIImage imageNamed:@"share_image"];

    if ([QQApiInterface isQQInstalled]) {
        
        UMSocialUrlResource *source = nil;
        if (self.shareInfo.regShareImg.length>0)
        {
            source = [[UMSocialUrlResource alloc]initWithSnsResourceType:UMSocialUrlResourceTypeImage url:self.shareInfo.regShareImg];
        }
        [UMSocialData defaultData].extConfig.qzoneData.title = self.shareInfo.regShareTitle.length>0?self.shareInfo.regShareTitle:@"金桔金融";
        [UMSocialData defaultData].extConfig.qzoneData.url = [NSString stringWithFormat:@"%@?uid=%@",self.shareInfo.regShareUrl,[HBLoginManager sharedHBLoginManager].currentUserInfo.uid];
        
        [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[UMShareToQzone] content:self.shareInfo.regShareDesc image:source?:test location:nil urlResource:source presentedController:self completion:^(UMSocialResponseEntity *response){
            if (response.responseCode == UMSResponseCodeSuccess) {
                NSLog(@"分享成功！");
            }
        }];
    }else{
        UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:@"您还没有安装手机QQ" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alter show];
    }
}


@end
